//
// Created by user on 2022/3/15.
//

#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
    };

ListNode* swapPairs(ListNode* head) {
    if(head== nullptr || head->next== nullptr)return head;
    ListNode *dummy = new ListNode(-1);
    dummy->next = head;

    ListNode *prev = head;
    ListNode *curr = head->next;
    while (curr){
        int tmp = curr->val;
        curr->val = prev->val;
        prev->val = tmp;
        prev = curr->next;
        if(!prev)break;
        curr = prev->next;
    }
    return dummy->next;

}

ListNode* swapPairs1(ListNode* head) {
    if (!head || !head->next) return head;

    ListNode* dummyNode = new ListNode(-1);
    dummyNode->next = head;

    ListNode *prev = dummyNode;
    ListNode *first = head;
    ListNode *second = head->next;
    while (second) {
        ListNode *next = second->next;
        prev->next = second;
        second->next = first;
        first->next = next;

        prev = first;
        first = next;
        if (!first) break;
        second = first->next;
    }
    return dummyNode->next;
}